/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jmex.model.ogredotscene.loader;

import com.jme.light.DirectionalLight;
import com.jme.light.PointLight;
import com.jme.light.SpotLight;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.state.LightState;
import java.util.List;
import com.jmex.model.ogredotscene.gen.Light;
import com.jmex.model.ogredotscene.gen.PointEnum;
import com.jmex.model.ogredotscene.util.XMLUtil;

/**
 *
 * @author czarnik_a
 */
public class LightLoader {

    private LightState lightState;

    public void addLight(Light light) {
        if (light.getType() == PointEnum.POINT) {
            PointLight pointLight = new PointLight();

            if (light.getColourDiffuse() != null) {
                pointLight.setDiffuse(new ColorRGBA(XMLUtil.getFloatString(light.getColourDiffuse().getR()), XMLUtil.getFloatString(light.getColourDiffuse().getG()), XMLUtil.getFloatString(light.getColourDiffuse().getB()), 0.0f));
            }
            if (light.getColourSpecular() != null) {
                pointLight.setSpecular(new ColorRGBA(XMLUtil.getFloatString(light.getColourSpecular().getR()), XMLUtil.getFloatString(light.getColourSpecular().getG()), XMLUtil.getFloatString(light.getColourSpecular().getB()), 0.0f));
            }

            if (light.getPosition() != null) {
                pointLight.setLocation(new Vector3f(XMLUtil.getFloatString(light.getPosition().getX()), XMLUtil.getFloatString(light.getPosition().getY()), XMLUtil.getFloatString(light.getPosition().getZ())));
            }
            pointLight.setEnabled(true);
            lightState.attach(pointLight);
        }

        if (light.getType() == PointEnum.SPOT_LIGHT) {
            SpotLight spot = new SpotLight();

            if (light.getColourDiffuse() != null) {
                spot.setDiffuse(new ColorRGBA(XMLUtil.getFloatString(light.getColourDiffuse().getR()), XMLUtil.getFloatString(light.getColourDiffuse().getG()), XMLUtil.getFloatString(light.getColourDiffuse().getB()), 0.0f));
            }
            if (light.getColourSpecular() != null) {
                spot.setSpecular(new ColorRGBA(XMLUtil.getFloatString(light.getColourSpecular().getR()), XMLUtil.getFloatString(light.getColourSpecular().getG()), XMLUtil.getFloatString(light.getColourSpecular().getB()), 0.0f));
            }
            if (light.getDirection() != null) {
                spot.setDirection(new Vector3f(XMLUtil.getFloatString(light.getDirection().getX()), XMLUtil.getFloatString(light.getDirection().getY()), XMLUtil.getFloatString(light.getDirection().getZ())));
            }
            if (light.getPosition() != null) {
                spot.setLocation(new Vector3f(XMLUtil.getFloatString(light.getPosition().getX()), XMLUtil.getFloatString(light.getPosition().getY()), XMLUtil.getFloatString(light.getPosition().getZ())));
            }
            spot.setEnabled(true);
            spot.setAngle(15f);
            lightState.attach(spot);
        }

        if (light.getType() == PointEnum.DIRECTIONAL) {
            DirectionalLight directionlLight = new DirectionalLight();

            if (light.getColourDiffuse() != null) {
                directionlLight.setDiffuse(new ColorRGBA(XMLUtil.getFloatString(light.getColourDiffuse().getR()), XMLUtil.getFloatString(light.getColourDiffuse().getG()), XMLUtil.getFloatString(light.getColourDiffuse().getB()), 0.0f));
            }
            if (light.getColourSpecular() != null) {
                directionlLight.setSpecular(new ColorRGBA(XMLUtil.getFloatString(light.getColourSpecular().getR()), XMLUtil.getFloatString(light.getColourSpecular().getG()), XMLUtil.getFloatString(light.getColourSpecular().getB()), 0.0f));
            }
            if (light.getDirection() != null) {
                directionlLight.setDirection(new Vector3f(XMLUtil.getFloatString(light.getDirection().getX()), XMLUtil.getFloatString(light.getDirection().getY()), XMLUtil.getFloatString(light.getDirection().getZ())));
            }
            directionlLight.setEnabled(true);
            lightState.attach(directionlLight);
        }

    }

    public void setLights(List<Light> lightList) {

        for (Light light : lightList) {
            addLight(light);
        }
    }

    public LightState getLightState() {
        return lightState;
    }

    public void setLightState(LightState lightState) {
        this.lightState = lightState;
    }
}
